package cn.iocoder.yudao.module.biz.controller.admin.expand.record;

import cn.iocoder.yudao.module.biz.controller.admin.expand.record.vo.ExpandRecordPageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.expand.record.vo.ExpandRecordRespVO;
import cn.iocoder.yudao.module.biz.controller.admin.expand.record.vo.ExpandRecordSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.biz.controller.admin.expand.record.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.expand.record.ExpandRecordDO;
import cn.iocoder.yudao.module.biz.service.expand.record.ExpandRecordService;

@Tag(name = "管理后台 - 拓展记录")
@RestController
@RequestMapping("/san/biz-expand-record")
@Validated
public class ExpandRecordController {

    @Resource
    private ExpandRecordService expandRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建拓展记录")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:create')")
    public CommonResult<Long> createExpandRecord(@Valid @RequestBody ExpandRecordSaveReqVO createReqVO) {
        return success(expandRecordService.createExpandRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拓展记录")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:update')")
    public CommonResult<Boolean> updateExpandRecord(@Valid @RequestBody ExpandRecordSaveReqVO updateReqVO) {
        expandRecordService.updateExpandRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拓展记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:delete')")
    public CommonResult<Boolean> deleteExpandRecord(@RequestParam("id") Long id) {
        expandRecordService.deleteExpandRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除拓展记录")
                @PreAuthorize("@ss.hasPermission('san:biz-expand-record:delete')")
    public CommonResult<Boolean> deleteExpandRecordList(@RequestParam("ids") List<Long> ids) {
        expandRecordService.deleteExpandRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拓展记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:query')")
    public CommonResult<ExpandRecordRespVO> getExpandRecord(@RequestParam("id") Long id) {
        ExpandRecordDO bizExpandRecord = expandRecordService.getExpandRecord(id);
        return success(BeanUtils.toBean(bizExpandRecord, ExpandRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拓展记录分页")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:query')")
    public CommonResult<PageResult<ExpandRecordRespVO>> getExpandRecordPage(@Valid ExpandRecordPageReqVO pageReqVO) {
        PageResult<ExpandRecordDO> pageResult = expandRecordService.getExpandRecordPage(pageReqVO);
        pageResult.getList().forEach(person -> {
            String status = "客户维护";
            String[] array = {"02_03","03_05","04_01","04_02"};
            if(Arrays.asList(array).contains(person.getExpandProcess())){
                status = "本月签约";
            }
            /*else if(person.getExpandProcess().startsWith("01")) {
                status = "拓展中";
            }*/
            person.setExpandStatus(status);
        });
        return success(BeanUtils.toBean(pageResult, ExpandRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出拓展记录 Excel")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExpandRecordExcel(@Valid ExpandRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExpandRecordDO> list = expandRecordService.getExpandRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "拓展记录.xls", "数据", ExpandRecordRespVO.class,
                        BeanUtils.toBean(list, ExpandRecordRespVO.class));
    }

    @GetMapping("/countByType")
    @Operation(summary = "根据类型获取拓展统计")
    @Parameter(name = "type", description = "类型", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:query')")
    public CommonResult<ExpandRecordCountRespVO> getExpandTarget(@RequestParam("type") int type) {
        return success(expandRecordService.countByType(type, getLoginUserId()));
    }

    @PostMapping("/countAllUserExpand")
    @Operation(summary = "获取所有拓展人的拓展统计")
    @Parameter(name = "id", description = "销售团队ID", required = true, example = "1")
    @Parameter(name = "month", description = "查询月份", required = true, example = "2025-08")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:query')")
    public CommonResult<List<ExpandRecordUserCountRespVO> > countAllUserExpand(@RequestParam("id") String id, @RequestParam("month") String month) {
        return success(expandRecordService.countAllUserExpand(Long.valueOf(id), month));
    }

    @PostMapping("/getExpandRecordCurrent")
    @Operation(summary = "查询拓展人的最新一条数据列表并获取数量")
    @Parameter(name = "id", description = "拓展人id", required = true, example = "1")
    @Parameter(name = "month", description = "查询月份", required = true, example = "2025-08")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:query')")
    public CommonResult<List<ExpandRecordRespVO>> getExpandRecordCurrent(@RequestParam("id") String id, @RequestParam("month") String month) {
        return success(expandRecordService.getExpandRecordCurrent(Long.valueOf(id), month));
    }

    @GetMapping("/getExpandRecordGk")
    @Operation(summary = "获取大屏项目概况")
//    @Parameter(name = "id", description = "拓展人id", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:query')")
    public CommonResult<Map<String,Object>> getExpandRecordGk() {
        return success(expandRecordService.getExpandRecordGk( ));
    }

    @GetMapping("/getListByCondition")
    @Operation(summary = "根据查询条件获取拓展记录")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:query')")
    public CommonResult<List<ExpandRecordRespVO>> getListByCondition(@Valid ExpandRecordPageReqVO pageReqVO) {
        List<ExpandRecordDO> dataList = expandRecordService.getListByCondition(pageReqVO);
        return success(BeanUtils.toBean(dataList, ExpandRecordRespVO.class));
    }

    @GetMapping("/getListByInterviewee")
    @Operation(summary = "根据受访人获取拓展记录")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:query')")
    public CommonResult<List<ExpandRecordRespVO>> getListByInterviewee(@Valid ExpandRecordPageReqVO pageReqVO) {
        List<ExpandRecordDO> dataList = expandRecordService.getListByInterviewee(pageReqVO);
        return success(BeanUtils.toBean(dataList, ExpandRecordRespVO.class));
    }

    @GetMapping("/checkRecordByCustomerName")
    @Operation(summary = "根据客户名称查询拓展记录")
    @Parameter(name = "customerName", description = "客户名称", required = true, example = "客户abc")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-record:query')")
    public CommonResult<List<ExpandRecordCheckRespVo>> checkRecordByCustomerName(@RequestParam("customerName") String customerName) {
        return success(expandRecordService.checkRecordByCustomerName(customerName));
    }

}
